using System;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using MSUtil;

namespace AjaxLabCmd
{
	/// <summary>
	/// Summary description for _default.
	/// </summary>
	public class defaulttest : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.DataGrid DataGrid1;
		protected System.Web.UI.WebControls.DataGrid DataGrid2;
		protected System.Web.UI.WebControls.Label TotalBandwidth;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			string strSQL = null;
			string log_dir = "";
			
			log_dir = ConfigurationSettings.AppSettings["Log_Dir"];

			strSQL = @"SELECT c-ip, Count(c-ip) as Total from "+log_dir+"*.log" +
				@" group by c-ip order by Total Desc";
			
			strSQL = @"SELECT  Count(c-ip) as Total from "+log_dir+"*.log";

			DataSet sets = this.parseLog(strSQL);
			this.DataGrid1.DataSource = sets;
			this.DataGrid1.DataBind();
			strSQL = @"SELECT c-ip, Count(c-ip) as Total from "+log_dir+"*.log" +
				@" group by c-ip order by Total Desc";
			sets = this.parseLog(strSQL);
			DataGrid2.DataSource = sets;
			DataGrid2.DataBind();
			this.TotalBandwidth.Text = sets.Tables[0].Rows.Count.ToString();
		}

		private DataSet parseLog(string query)
		{
			LogQueryClassClass logParser = new LogQueryClassClass();
			COMIISW3CInputContextClassClass iisLog = new COMIISW3CInputContextClassClass();

			ILogRecordset rsLP = null;
			ILogRecord rowLP = null;
			
			rsLP = logParser.Execute(query, iisLog);

			DataTable tab = new DataTable("Results");

			// copy schema
			for (int i = 0; i < rsLP.getColumnCount(); i++)
			{
				DataColumn col = new DataColumn();
				col.ColumnName = rsLP.getColumnName(i);
				switch (rsLP.getColumnType(i))
				{
					case 1:
						col.DataType = Type.GetType("System.Int32");
						break;
					case 2:
						col.DataType = Type.GetType("System.Double");
						break;
					case 4:
						col.DataType = Type.GetType("System.DateTime");
						break;
					default:
						col.DataType = Type.GetType("System.String");
						break;
				}
				tab.Columns.Add(col);
			}

			// copy data
			while (!rsLP.atEnd())
			{
				rowLP = rsLP.getRecord();
				DataRow row = tab.NewRow();

				for (int i = 0; i < rsLP.getColumnCount(); i++)
					row[i] = HttpUtility.HtmlEncode(Convert.ToString(rowLP.getValue(i)));

				tab.Rows.Add(row);
				rsLP.moveNext();
			}

			DataSet ds = new DataSet();
			ds.Tables.Add(tab);

			return ds;
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
